package com.example.chatservice.webSocket.netty;

import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description
 * @Author susu
 * @Date 2024/9/18
 */
@Slf4j
public class HandlerHeartBeat extends ChannelDuplexHandler {
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent){
            IdleStateEvent e = (IdleStateEvent) evt;
            if (e.state() == IdleState.READER_IDLE){
                Channel channel = ctx.channel();
                String id = channel.id().toString();

                Attribute<String> attr = channel.attr(AttributeKey.valueOf(id));
                String userId = attr.get();
                log.info("{}心跳超时",userId);
                ctx.close();
            }else if (e.state() == IdleState.WRITER_IDLE){
                ctx.writeAndFlush("heart");
            }

        }
    }
}
